package Include;

import java.util.HashMap;
import java.util.Map;

// 表示符号表中的作用域（Scope）
// 它支持符号的定义、删除、查找，并支持作用域的嵌套。
public class Scope {
    public Scope enclosingScope;
    public Map<String, Symbol> fun_symbols;
    public Map<String, Symbol> para_symbols;

    public Scope(){
        this.enclosingScope = null;
        this.fun_symbols = new HashMap<>();
        this.para_symbols  = new HashMap<>();
    }

    public Scope(Scope enclosingScope){
        this.enclosingScope = enclosingScope;
        this.fun_symbols = new HashMap<>();
        this.para_symbols  = new HashMap<>();
    }

    public void fun_define(Symbol sym){
        this.fun_symbols.put(sym.name, sym);
    }

    public void para_define(Symbol sym){
        this.para_symbols.put(sym.name, sym);
    }

    public void para_delete(String name){
        this.para_symbols.remove(name);
    }

    public Symbol resolve(String name){
        if(this.fun_symbols.containsKey(name)) {
            return this.fun_symbols.get(name);
        }else if(this.para_symbols.containsKey(name)) {
            return this.para_symbols.get(name);
        }else if(this.enclosingScope !=null) {
            return this.enclosingScope.resolve(name);
        }
        return null;
    }

    public boolean cur_resolve(String name){
        return (fun_symbols.containsKey(name) || para_symbols.containsKey(name));
    }
}

